package middleware

import (
	"gitee.com/pearl-zz/go-manage-system-core/sdk/config"
	"gitee.com/pearl-zz/go-manage-system-core/sdk/pkg/jwtauth"
	"gitee.com/pearl-zz/go-manage-system/common/middleware/handler"
	"time"
)

// AuthInit jwt 验证
func AuthInit() (*jwtauth.GinJwtMiddleware, error) {
	timeout := time.Hour
	if config.ApplicationConfig.Mode == "dev" {
		timeout = time.Duration(876010) * time.Hour
	} else {
		if config.JwtConfig.Timeout != 0 {
			timeout = time.Duration(config.JwtConfig.Timeout) * time.Second
		}
	}

	return jwtauth.New(&jwtauth.GinJwtMiddleware{
		Realm:           "test zone",
		Key:             []byte(config.JwtConfig.Secret),
		Timeout:         timeout,
		MaxRefresh:      time.Hour,
		Authenticator:   handler.Authenticator,
		Authorizator:    handler.Authorizator,
		PayloadFunc:     handler.PayloadFunc,
		Unauthorized:    handler.Unauthorized,
		IdentityHandler: handler.IdentityHandler,
		TokenLookup:     "header: Authorization, query: token, cookie: jwt",
		TokenHeadName:   "Bearer",
		TimeFunc:        time.Now,
	})
}
